package club.cearnach.api.dao;

import club.cearnach.entity.Movie;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * @author 阮胜
 * @date 2018/5/4 19:37
 */
public interface IMovieDao extends JpaRepository<Movie, Integer> {
    List<Movie> findByName(String name);

    List<Movie> findByTypeId(Integer id);

    List<Movie> findByMovieLanguageId(Integer id);

    List<Movie> findAllByRate(Pageable pageable);

    Long countMovieByTypeId(Integer id);

    Page<Movie> findAllByOrderByRateDesc(Pageable pageable);

    Page<Movie> findAllByOrderByUpdateDateDesc(Pageable pageable);

    Page<Movie> findAllByOrderByCreateDateDesc(Pageable pageable);

    Page<Movie> findAllByOrderByInitialReleaseDateDesc(Pageable pageable);

    Page<Movie> findAllByTypeIdOrderByCreateDateDesc(Integer typeId, Pageable pageable);

    @Query(value = "SELECT * FROM MOVIE WHERE " +
            "MOVIE_NAME LIKE %?1% " +
            "OR MOVIE_TYPE_ID = ?2 " +
            "OR MOVIE_RATE >= ?3 " +
            "OR YEAR(MOVIE_INITIAL_RELEASE_DATE) BETWEEN ?4 AND ?5 ORDER BY CREATE_DATE DESC", nativeQuery = true)
    Page<Movie> findAllByCondition(String movieName, Integer typeId, Float rate, Integer startYear, Integer endYear, Pageable pageable);


}
